package com.supreme.uploadserver;

import com.sun.mail.util.BASE64DecoderStream;

import javax.mail.*;
import javax.mail.event.FolderAdapter;
import javax.mail.event.FolderEvent;
import javax.mail.event.StoreEvent;
import javax.mail.event.StoreListener;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

public class ReceiveMailServer {

    public static void printInboxFolder(Store store) throws MessagingException, IOException {
        // 获得收件箱
        Folder folder = store.getFolder("INBOX");
        // 以读写模式打开收件箱
        folder.open(Folder.READ_WRITE);

        // 打印不同状态的邮件数量
        int messageCount = folder.getMessageCount();
        System.out.println("收件箱中共" + messageCount + "封邮件!");
        System.out.println("收件箱中共" + folder.getUnreadMessageCount() + "封未读邮件!");
        int newMessageCount = folder.getNewMessageCount();
        System.out.println("收件箱中共" + folder.getNewMessageCount() + "封新邮件!");
        System.out.println("收件箱中共" + folder.getDeletedMessageCount() + "封已删除邮件!");

        // 获得收件箱的邮件列表
//        Message[] messageArray = folder.getMessages();
//        Message[] messageArray = folder.getMessages(messageCount, messageCount);
        Message[] messageArray = folder.getMessages(messageCount - newMessageCount + 1, messageCount);
        System.out.println("收件箱中共" + messageArray.length + "封邮件!");

        System.out.println("------------------------开始解析邮件----------------------------------");
        // 得到收件箱文件夹信息，获取邮件列表

        for (int i = 0; i < messageArray.length; i++) {
            MimeMessage messageItem = (MimeMessage) messageArray[i];
            //   获取邮箱邮件名字及时间
            final String subject = messageItem.getSubject();
            final Multipart multipart = (Multipart) messageItem.getContent();
            System.out.println("subject: " + subject);

            for (int j = 0; j < multipart.getCount(); j++) {
                final BodyPart bodyPart = multipart.getBodyPart(j);
                final String contentType = bodyPart.getContentType();
                final Object content = bodyPart.getContent();

                System.out.println(j + " ------------------------------------------------");
                /*if (contentType.startsWith("application/pdf")) {
                    System.out.printf("contentType: %s\ncontent: %s\n", contentType, content);
                    BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) content;
                } else if (contentType.equals("text/html; charset=UTF-8")) {
                    System.out.printf("contentType: %s\ncontent: %s\n", contentType, content);
                }*/
                System.out.printf("contentType: %s\ncontent: %s\n", contentType, content);
                System.out.println(j + " ------------------------------------------------");
            }

            System.out.println("==============");
        }
        System.out.println("----------------End------------------");

        // 关闭资源
        folder.close(false);
        store.close();
    }

    public static void addEventListener(Store store) {
        store.addStoreListener(new StoreListener() {
            @Override
            public void notification(StoreEvent storeEvent) {
                final String message = storeEvent.getMessage();
                System.out.println("message: " + message);
            }
        });
        store.addFolderListener(new FolderAdapter() {
            @Override
            public void folderCreated(FolderEvent e) {
                super.folderCreated(e);
            }

            @Override
            public void folderRenamed(FolderEvent e) {
                super.folderRenamed(e);
            }

            @Override
            public void folderDeleted(FolderEvent e) {
                super.folderDeleted(e);
            }
        });
    }

    public static void main(String[] args) {
        // 准备连接服务器的会话信息
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imap");
        props.setProperty("mail.imap.host", "smtp.citec-c.com");
        props.setProperty("mail.imap.port", "143");

        // 创建Session实例对象
        Session session = Session.getInstance(props);

        try {
            // 创建IMAP协议的Store对象
            Store store = session.getStore("imap");

            // 连接邮件服务器
            store.connect("qu-ming@citec-c.com", "cyber1234");

//            addEventListener(store);
            printInboxFolder(store);
        } catch (MessagingException | IOException e) {
            e.printStackTrace();
        }


        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            String result = scanner.next();
            System.out.println("result: " + result);
        }

        System.out.println("finish");
    }
}
